<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item
        :label="$t('payroll_calculation_name')"
        prop="calculationName"
      >
        <el-input
          v-model="queryParams.calculationName"
          :placeholder="
            $t('please_enter', { name: $t('payroll_calculation_name') })
          "
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        :label="$t('calculation_type')"
        prop="salaryCalculationType"
      >
        <el-select
          v-model="queryParams.salaryCalculationType"
          :placeholder="$t('please_select', { name: $t('calculation_type') })"
          clearable
        >
          <el-option
            v-for="dict in dict.type.salary_calculation_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['salary:salaryCalculation:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="operationList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('payroll_calculation_name')"
        align="center"
        prop="calculationName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('calculation_date')"
        align="center"
        prop="confirmedTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('calculation_type')"
        align="center"
        prop="salaryCalculationType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.salary_calculation_type"
            :value="scope.row.salaryCalculationType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('calculation_cycle')"
        align="center"
        prop="salaryCycleName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('payroll_group')"
        align="center"
        prop="salaryGroupId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="salaryGroupOptions"
            :value="scope.row.salaryGroupId"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('heacounts_of_calcualtion')"
        align="center"
        prop="calculationNumberPeople"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('total_net_payment_of_salary')"
        align="center"
        prop="calculationTotalMoney"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('calculation_status')"
        align="center"
        prop="calculationStatus"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.salary_calculation_status"
            :value="scope.row.calculationStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('payslip')"
        align="center"
        prop="salarySheet"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('publisher')"
        align="center"
        prop="updateBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('operation_time')"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['salary:salaryCalculation:view']"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['salary:salaryCalculation:edit']"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lock"
            @click="handleLock(scope.row)"
            v-hasPermi="['salary:salaryCalculation:lock']"
            v-if="
              scope.row.calculationStatus == 3 ||
              scope.row.calculationStatus == 4
            "
            >{{ $t("locked") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="handleCreateSalaryReport(scope.row)"
            v-hasPermi="['salary:salaryCalculation:report']"
            v-if="
              scope.row.calculationStatus == 5 ||
              scope.row.calculationStatus == 6
            "
            >{{ $t("generate_report") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['salary:salaryCalculation:delete']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改薪酬运算列表对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
      :before-close="backList"
    >
      <el-steps :active="stepActive" align-center>
        <el-step
          :title="$t('payroll_period')"
          @click.native="lookView ? changeStep(1) : ''"
        ></el-step>
        <el-step
          :title="$t('amount_adjustment')"
          @click.native="lookView ? changeStep(2) : ''"
        ></el-step>
        <el-step
          :title="$t('calculate')"
          @click.native="lookView ? changeStep(3) : ''"
        ></el-step>
        <el-step
          :title="$t('calculation_completed')"
          @click.native="lookView ? changeStep(4) : ''"
        ></el-step>
      </el-steps>
      <el-form :disabled="lookView">
        <step1
          :form="form"
          :salaryGroupOptions="salaryGroupOptions"
          :salaryProjectOptions="salaryProjectOptions"
          :salaryCycleOptions="salaryCycleOptions"
          @saveSalaryRangeFinish="saveSalaryRangeFinish"
          @changeSalaryGroup="changeSalaryGroup"
          v-if="stepActive == 1"
        ></step1>
        <step2
          :salaryCalculationForm="form"
          @changeStatus="changeStatus"
          @changeStep="changeStep"
          v-if="stepActive == 2"
        ></step2>
        <step3
          :salaryCalculationForm="form"
          @changeStatus="changeStatus"
          @operationFinish="refreshDetail"
          v-if="stepActive == 3"
        ></step3>
        <step4
          :salaryCalculationForm="form"
          @changeStatus="changeStatus"
          @backList="backList"
          v-if="stepActive == 4"
        ></step4>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOperation,
  getOperation,
  delOperation,
  saveSalaryCalculation,
  createSalaryReport,
} from "@/api/salary/salaryCalculation";
import {
  listAllSalaryGroup,
  getSalaryGroupProjectList,
  getSalaryGroupSalaryCycleList,
} from "@/api/salary/salaryGroup";
import step1 from "./step/salaryRange.vue";
import step2 from "./step/amountChange.vue";
import step3 from "./step/operation.vue";
import step4 from "./step/operationComplete.vue";
export default {
  name: "Operation",
  dicts: [
    "salary_calculation_type",
    "approval_status",
    "salary_calculation_status",
  ],
  components: {
    step1,
    step2,
    step3,
    step4,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪酬运算列表表格数据
      operationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operationcName: null,
        operationalType: null,
        approver: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      stepActive: 1,
      salaryGroupOptions: [],
      salaryProjectOptions: [],
      salaryCycleOptions: [],
      lookView: false,
    };
  },
  watch: {
    "form.salaryGroupId": {
      handler(val) {
        if (val) {
          this.getSalaryGroupProjectList();
          this.getSalaryGroupSalaryCycleList();
        } else {
          this.salaryProjectOptions = [];
          this.salaryCycleOptions = [];
        }
      },
      immediate: true,
    },
  },
  created() {
    this.getList();
    this.listAllSalaryGroup();
  },
  methods: {
    /** 查询薪酬运算列表列表 */
    getList() {
      this.loading = true;
      listOperation(this.queryParams).then((response) => {
        this.operationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        calculationName: null,
        salaryGroupId: null,
        salaryCalculationType: null,
        calculationMonth: null,
        salaryGroupCycleId: null,
        salaryCycleName: null,
        startMonthType: null,
        startDate: null,
        endMonthType: null,
        endDate: null,
        temporaryCalculationReason: null,
        paymentMethod: null,
        currencyType: null,
        isIncludeTemporary: null,
        isCalculation: null,
      };
      this.stepActive = 1;
      this.lookView = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("add_new_payroll_calculation");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOperation(id).then((response) => {
        this.form = response.data;
        if (this.form.calculationStatus == 1) {
          this.changeStep(2);
        } else if (this.form.calculationStatus == 2) {
          this.changeStep(3);
        } else {
          this.changeStep(4);
        }
        this.open = true;
        this.title = this.$t("edit_payroll_calculation");
      });
    },
    refreshDetail(row) {
      const id = row.id;
      getOperation(id).then((response) => {
        this.form = response.data;
        if (this.form.calculationStatus == 1) {
          this.changeStep(2);
        } else if (this.form.calculationStatus == 2) {
          this.changeStep(3);
        } else {
          this.changeStep(4);
        }
      });
    },
    /**查看操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      getOperation(id).then((response) => {
        this.form = response.data;
        if (this.form.calculationStatus == 1) {
          this.changeStep(2);
        } else if (this.form.calculationStatus == 2) {
          this.changeStep(3);
        } else {
          this.changeStep(4);
        }
        this.open = true;
        this.lookView = true;
        this.title = this.$t("view");
      });
    },
    /** 锁定 */
    handleLock(row) {
      saveSalaryCalculation({
        calculationStatus: 5,
        salaryCalculationId: row.id,
      }).then((res) => {
        this.$modal.msgSuccess(this.$t("operation_successful"));
        this.getList();
      });
    },
    /**生成报告 */
    handleCreateSalaryReport(row) {
      this.$modal
        .confirm(this.$t("generating_salary_report"))
        .then(() => {
          createSalaryReport(row.id).then((res) => {
            this.$modal.msgSuccess(this.$t("operation_successful"));
          });
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delOperation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "operation/operation/export",
        {
          ...this.queryParams,
        },
        `operation_${new Date().getTime()}.xlsx`
      );
    },
    /*查询所有薪酬组*/
    listAllSalaryGroup() {
      listAllSalaryGroup().then((res) => {
        this.salaryGroupOptions = res.data.map((item) => {
          return {
            value: item.id,
            label: item.groupName,
            raw: {
              listClass: "",
            },
          };
        });
      });
    },
    /*薪酬组——薪酬项目*/
    getSalaryGroupProjectList() {
      getSalaryGroupProjectList({
        salaryGroupId: this.form.salaryGroupId,
      }).then((res) => {
        this.salaryProjectOptions = res.data;
      });
    },
    /*薪酬组——算新周期*/
    getSalaryGroupSalaryCycleList() {
      getSalaryGroupSalaryCycleList({
        salaryGroupId: this.form.salaryGroupId,
      }).then((res) => {
        this.salaryCycleOptions = res.data.map((item) => {
          return {
            value: item.id,
            label: item.salaryCycleName,
            raw: {
              listClass: "",
            },
          };
        });
      });
    },
    /*薪酬组选择改变*/
    changeSalaryGroup() {
      this.form.salaryGroupCycleId = null;
    },
    /*计薪范围完成*/
    saveSalaryRangeFinish(id) {
      this.form.id = id;
      this.refreshDetail(this.form);
    },
    /*调整状态 */
    changeStatus(value) {
      saveSalaryCalculation({
        calculationStatus: value,
        salaryCalculationId: this.form.id,
      }).then((res) => {
        this.refreshDetail(this.form);
      });
    },
    changeStep(value) {
      if (!this.form.id) {
        return;
      }
      this.stepActive = value;
    },
    /*第四步 返回列表 */
    backList() {
      this.open = false;
      this.getList();
    },
  },
};
</script>
